﻿using UnityEngine;
using UnityEngine.Events;

namespace Jinndev {

    /// <summary>
    /// Update方法代理，可以切换Update、LateUpdate、FixedUpdate，指定是否使用Unscaled的deltaTime
    /// </summary>
    public class UpdateAgent : MonoBehaviour {

        public UpdateMethod updateMethod = UpdateMethod.Update;
        public bool useUnscaledDeltaTime = false;

        public UnityEvent<float> onUpdate = new UnityEvent<float>();

        private void Update() {
            if (updateMethod == UpdateMethod.Update) {
                onUpdate.Invoke(useUnscaledDeltaTime ? Time.unscaledDeltaTime : Time.deltaTime);
            }
        }

        private void LateUpdate() {
            if (updateMethod == UpdateMethod.LateUpdate) {
                onUpdate.Invoke(useUnscaledDeltaTime ? Time.unscaledDeltaTime : Time.deltaTime);
            }
        }

        private void FixedUpdate() {
            if (updateMethod == UpdateMethod.FixedUpdate) {
                onUpdate.Invoke(useUnscaledDeltaTime ? Time.fixedUnscaledDeltaTime : Time.fixedDeltaTime);
            }
        }

    }

}
